--[[ 能源之岚(日)
  特点是横屏,而且Y坐标上下倒转.直接用read_u16的方法即可.
  
  改装到MyMame模块和MySTG模块
  
  By setycyas @2025-02-12
]]

--防止重复运行
if (_G.game) then
  print('script already loaded!')
  return
end

local myMame = require('MyMame')
local myStg = require('MySTG')

-- 这里设定基本参数
local GAME = {
-- 脚本名,防重加载用
  NAME = "progearj_autoboot",
  -- 分辨率
  X_RESOLUTION = 384,
  Y_RESOLUTION = 224,
  -- 横坐标纵坐标内存地址
  X_ADDR_HIGH = 0xffff8e,
  X_ADDR_LOW = 0xffff8f,
  Y_ADDR_HIGH = 0xffff8c,
  Y_ADDR_LOW = 0xffff8d,
  -- 如果仅用read_u16读取坐标和计算,就设定为true
  USE_U16 = true,
  -- 坐标修正像素
  X_OFFSET = 0,
  Y_OFFSET = 0,
  -- 自己中心方块的半长半高,单位为MAME的屏幕比例
  --BOX_HALF_WIDTH = 0.005,
  --BOX_HALF_HEIGHT = 0.005,
  BOX_WIDTH = 4,
  BOX_HEIGHT = 3,
  -- 中心方块的边框颜色和中心颜色
  BOX_BORDER_COLOR = 0x88ff0000,
  BOX_COLOR = 0x88ff0000,
  -- 十字线宽度和颜色
  LINE_WIDTH = 1.3,
  LINE_COLOR = 0xFFff00ff,
  -- 中途需要变更的测试参数,准备了10个
  TEST_PARAMS = {64.0, 64.0, 208.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
}
-- 更新一下模块的参数
myMame.gameName = GAME.NAME
myMame.screenIndex = ':screen'
myStg.screenIndex = ':screen'
myStg.X_RESOLUTION = GAME.X_RESOLUTION
myStg.Y_RESOLUTION = GAME.Y_RESOLUTION

-- 坐标变换函数,从得到的坐标变为屏幕显示位置(是个比例),返回0.0-1.0的浮点数,
-- 后期游戏可能很复杂,需要根据游戏的情况手动操作变化
function GAME.getX_u8()
  local xhigh, xlow, result
  xhigh = 0 
  xlow = 0
  if (GAME.X_ADDR_HIGH > 0) then
    xhigh = myMame.mem:read_u8(GAME.X_ADDR_HIGH)
  end
  if (GAME.X_ADDR_LOW > 0) then
    xlow = myMame.mem:read_u8(GAME.X_ADDR_LOW)
  end
  result = 4.0*xhigh
  result = result+math.floor(4.0*xlow/0x100)
  
  result = result+GAME.X_OFFSET
  return result
end
function GAME.getY_u8()
  local ylow, yhigh, result
  yhigh = 0
  ylow = 0
  if (GAME.Y_ADDR_HIGH > 0) then
    yhigh = myMame.mem:read_u8(GAME.Y_ADDR_HIGH)
  end
  if (GAME.Y_ADDR_LOW > 0) then
    ylow = myMame.mem:read_u8(GAME.Y_ADDR_LOW)
  end
  result = 4.0*yhigh
  result = result+math.floor(4.0*ylow/0x100)
  
  result = result+GAME.Y_OFFSET
  return result
end

function GAME.getX_u16()
  local x
  if (GAME.X_ADDR_HIGH > 0) then
    x = 4.0*myMame.mem:read_u16(GAME.X_ADDR_HIGH)/0x100 --低位是小数部分,要/256
  else
    return -1
  end
  
  --实际像素位置就是x的4倍可能四舍五入!最后计算屏幕比例才除以分辨率!
  result = math.floor(x)+GAME.X_OFFSET
  
  return result
end
function GAME.getY_u16()
  local y
  if (GAME.Y_ADDR_HIGH > 0) then
    y = 4.0*myMame.mem:read_u16(GAME.Y_ADDR_HIGH)/0x100 --低位是小数部分,要/256
  else
    return -1
  end
  
  --实际像素位置就是y的4倍可能四舍五入!最后计算屏幕比例才除以分辨率!
  result = GAME.Y_RESOLUTION-(math.floor(y)+GAME.Y_OFFSET)
  
  return result
end

-- 监视数据显示
function GAME.check()
end

-- 显示自机的几个方框,xfloat,yfloat是浮点数坐标
-- 显示中心
function GAME.showCenter(x, y)

  myStg.centerBox(
    x, y,
    GAME.BOX_WIDTH, GAME.BOX_HEIGHT,
    GAME.BOX_BORDER_COLOR, GAME.BOX_COLOR
  )
  
end
  
-- 显示十字线
function GAME.showCrossLine(x, y)

  myStg.xLine(x, GAME.LINE_COLOR, GAME.LINE_WIDTH)
  myStg.yLine(y, GAME.LINE_COLOR, GAME.LINE_WIDTH) 
    
end

--[[ **************************
  主函数,绑定后每一帧执行一次该函数
]]
function GAME.Main()
  local x, y

  -- 获取坐标
  if GAME.USE_U16 then
    x = GAME.getX_u16()
    y = GAME.getY_u16()
  else
    x = GAME.getX_u8()
    y = GAME.getY_u8()
  end
  
  -- 数据监视
  --GAME.check()
  -- 显示角色中心辅助线
  if (x > 0) and (y > 0) then
    -- 十字线
    GAME.showCrossLine(x, y)
    -- 中心
    GAME.showCenter(x, y)
  end
  
end

--[[运行]]

-- 绑定每一帧执行的函数,测试时绑定到debug
--myMame.debugRunPerFrame(GAME.Main, 'progearj')
myMame.runPerFrame(GAME.Main, 'progearj')
-- 导出到全局变量
_G.game = GAME

return GAME
